-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[NFC][TableGen] Add IfGuardEmitter and adopt it in InstrInfoEmitter #168616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add a RAII `IfGuardEmitter` to insert simple #if guards and adopt it in InstrInfoEmitter.
🐧 Linux x64 Test Results
|
Member
|
@llvm/pr-subscribers-tablegen Author: Rahul Joshi (jurahul) ChangesAdd a RAII Full diff: https://github.com/llvm/llvm-project/pull/168616.diff 2 Files Affected:
diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h
index 1b1b5e63a8fc4..f7e732a4b232d 100644
--- a/llvm/include/llvm/TableGen/CodeGenHelpers.h
+++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h
@@ -46,6 +46,24 @@ class IfDefEmitter {
bool LateUndef;
};
+// Simple RAII helper for emitting #if guard. It emits:
+// <IfGuard> <Condition>
+// #endif // <Condition>
+class IfGuardEmitter {
+public:
+ IfGuardEmitter(raw_ostream &OS, StringRef Condition,
+ StringRef IfGuard = "#if")
+ : Condition(Condition.str()), OS(OS) {
+ OS << IfGuard << " " << Condition << "\n\n";
+ }
+
+ ~IfGuardEmitter() { OS << "\n#endif // " << Condition << "\n\n"; }
+
+private:
+ std::string Condition;
+ raw_ostream &OS;
+};
+
// Simple RAII helper for emitting header include guard (ifndef-define-endif).
class IncludeGuardEmitter {
public:
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 32994c12aa98b..0d13a6f173934 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -911,24 +911,23 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
}
}
- OS << "#if defined(GET_INSTRINFO_MC_DESC) || "
- "defined(GET_INSTRINFO_CTOR_DTOR)\n";
-
- OS << "namespace llvm {\n\n";
-
- OS << "struct " << TargetName << "InstrTable {\n";
- OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
- OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
- "\"Unwanted padding between Insts and OperandInfo\");\n";
- OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
- OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
- "\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
- OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) << "];\n";
- OS << "};\n\n";
-
- OS << "} // end namespace llvm\n";
- OS << "#endif // defined(GET_INSTRINFO_MC_DESC) || "
- "defined(GET_INSTRINFO_CTOR_DTOR)\n\n";
+ {
+ IfGuardEmitter IfGuard(
+ OS,
+ "defined(GET_INSTRINFO_MC_DESC) || defined(GET_INSTRINFO_CTOR_DTOR)");
+ NamespaceEmitter NS(OS, "llvm");
+
+ OS << "struct " << TargetName << "InstrTable {\n";
+ OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n";
+ OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), "
+ "\"Unwanted padding between Insts and OperandInfo\");\n";
+ OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n";
+ OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), "
+ "\"Unwanted padding between OperandInfo and ImplicitOps\");\n";
+ OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U)
+ << "];\n";
+ OS << "};";
+ }
const CodeGenRegBank &RegBank = Target.getRegBank();
const CodeGenHwModes &CGH = Target.getHwModes();
|
77a5078 to
ae51f61
Compare
s-barannikov
approved these changes
Nov 18, 2025
Contributor
s-barannikov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
arsenm
reviewed
Nov 19, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add a RAII
IfGuardEmitterto insert simple #if guards and adopt it in InstrInfoEmitter.